package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author Fengkai
 * @creat 2023/4/24
 * 组成最大数
 * 小组中每位都有一张卡片，卡片上是 6 位内的正整数，将卡片连起来可以组成多种数字，计算组成的最大数字。
 * <p>
 * 输入输出描述:
 * 输入描述:
 * “,” 号分割的多个正整数字符串，不需要考虑非数字异常情况，小组最多 25 个人
 */
public class GetMaxInt {
    public static void main (String[] args) {
        PriorityQueue<String> strings = new PriorityQueue<>(new Comparator<String>() {
            @Override
            public int compare (String o1, String o2) {
                int l1 = o1.length();
                int l2 = o2.length();
                int min = Math.min(l1, l2);
                Integer i1 = Integer.valueOf(o1.substring(0, min));
                Integer i2 = Integer.valueOf(o2.substring(0, min));
                if (i1 > i2) {
                    return -1;
                } else if (i1 < i2) {
                    return 1;
                }
                if (i1.compareTo(i2) == 0) {
                    if (l1 > l2){
                        String subL1 = o1.substring(min);
                        char[] chars1 = subL1.toCharArray();
                        char[] chars2 = o2.toCharArray();
                        int index = 0;
                        while (index < chars1.length && index < chars2.length){
                            if (chars1[index] > chars2[index]){
                                return -1;
                            }else if (chars1[index] < chars2[index]){
                                return 1;
                            }
                            index++;
                        }
                        return l1 - l2;
                    }else if (l1 < l2){
                        String subL2 = o2.substring(min);
                        char[] chars2 = subL2.toCharArray();
                        char[] chars1 = o1.toCharArray();
                        int index = 0;
                        while (index < chars1.length && index < chars2.length){
                            if (chars2[index] > chars1[index]){
                                return 1;
                            }else if (chars2[index] < chars1[index]){
                                return -1;
                            }
                            index++;
                        }
                        return l1 - l2;
                    }
                }
                return 0;
            }
        });

//4589,101,41425,9999
        strings.offer("458945899");
        strings.offer("4589");
        strings.offer("101");
        strings.offer("9999");
        while (strings.size() > 0) {
            System.out.println(strings.poll());
        }
    }
}
